10 RANDOMIZE TIMER:KEY OFF:WIDTH 40:DEFINT A-Z:DIM A(4),P(4),C(9,1)
20 WHILE E=0:CLS:LOCATE 4,4,1:PRINT "*** Bulls and Cows Mastermind ***";
30 T=0:N=INT(RND*10000) MOD 9999:A(0)=N MOD 10:A(1)=(N\10) MOD 10
31 A(2)=(N\100) MOD 10:A(3)=N\1000
40 WHILE E=0:LOCATE 6,15:PRINT SPC(24);
50 LOCATE 6,15:PRINT "Guess: ":GOSUB 300
60 IF E=2 THEN GOTO 150
70 A=0:B=0:FOR I=0 TO 3:D0=A(I):D1=P(I)
80 IF D0=D1 THEN A=A+1 ELSE C(D0,0)=C(D0,0)+1:C(D1,1)=C(D1,1)+1
90 NEXT I
100 FOR I=0 TO 9:D0=0:D1=0:SWAP D0,C(I,0):SWAP D1,C(I,1)
110 IF D0<D1 THEN B=B+D0 ELSE B=B+D1
120 NEXT I
125 T=T+1:LOCATE 8+T,15:PRINT USING "#. ";T;
130 PRINT USING "#";P(0);P(1);P(2);P(3);:PRINT USING " #A#B";A;B
140 IF A=4 AND B=0 THEN E=1 ELSE IF T>=8 THEN E=2
150 WEND
160 IF E=2 THEN LOCATE 19,7:PRINT "You Lose! The answer is: ";:PRINT USING "#";A(0);A(1);A(2);A(3);:SOUND 100,2
170 IF E=1 THEN LOCATE 19,17:PRINT "You win!";:SOUND 500,1:SOUND 1000,1
180 IF E>0 THEN LOCATE 21,13:PRINT "Play again? (Y/N)";
190 WHILE E>0:K$=INPUT$(1)
200 IF K$="y" OR K$="Y" THEN E=0 ELSE IF K$="n" OR K$="N" THEN E=-1
210 WEND
220 WEND
230 CLS:WIDTH 80:END
300 L=0:WHILE 1
310 LOCATE 6,22+L:K=ASC(INPUT$(1))
320 IF K=27 THEN E=2:RETURN
330 IF K=13 AND L=4 THEN RETURN
340 IF K=8 AND L>0 THEN L=L-1:LOCATE 6,22+L:PRINT " ";:LOCATE 6,22+L
350 IF K>=48 AND K<=57 AND L<4 THEN PRINT CHR$(K);:P(L)=K-48:L=L+1
360 WEND

